﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data
Imports System.Windows.Forms
Imports DevComponents.DotNetBar.Controls
Imports DevComponents.Editors.DateTimeAdv
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.DTO
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.BUSS

Namespace DoAnQuanLyBanHang.DAO
    Public Class DatHangCtrl
        Private m_DatHangData As New DatHangData()
        Private m_DatHangInfo As New DatHangInfo()


        Public Sub HienThiComboBox(cbx As ComboBox)
            Dim m_DT As DataTable = m_DatHangData.LayDatHang()
            cbx.DataSource = m_DT
            cbx.DisplayMember = "MaPhieu"

            cbx.ValueMember = "MaPhieu"
        End Sub
#Region "hien thi"
        Public Sub HienThi(Dgv As DataGridView, bvn As BindingNavigator)
            Dim bs As New BindingSource()

            bs.DataSource = m_DatHangData.LayDatHang()
            bvn.BindingSource = bs
            Dgv.DataSource = bs
        End Sub

        Public Shared Function LayDsDatHangTheoMa(MaPhieu As [String]) As IList(Of DatHangInfo)
            Dim ds As IList(Of DatHangInfo) = New List(Of DatHangInfo)()

            Dim data As New DatHangData()
            Dim tbl As DataTable = data.LayDSDatHangTheoMa(MaPhieu)

            Dim DatHang As IList(Of DatHangInfo) = New List(Of DatHangInfo)()

            For Each row As DataRow In tbl.Rows
                Dim DH As New DatHangInfo()

                Dim m_MH As New MatHangInfo()
                m_MH.TenMH = Convert.ToString(row("TenMH"))
                m_MH.MaMH = Convert.ToString(row("MaMH"))
                Dim m_KH As New KhachHangInfo()
                m_KH.HoTen = Convert.ToString(row("HoTen"))
                m_KH.MaKH = Convert.ToString(row("MaKH"))
                Dim m_LoaiTien As New LoaiTienInfo()
                m_LoaiTien.LoaiTien = Convert.ToString(row("LoaiTien"))
                Dim m_LoaiGD As New LoaiGiaoDichInfo()
                m_LoaiGD.TenGiaoDich = Convert.ToString(row("TenLoaiGiaoDich"))

                DH.MaPhieu = Convert.ToString(row("MaPhieu"))
                DH.ThanhToan = Convert.ToBoolean(row("ThanhToan"))
                DH.SoLuong = Convert.ToString(row("SoLuong"))
                DH.GiaBan = Convert.ToString(row("GiaBan"))
                DH.ThanhTien = Convert.ToString(row("ThanhTien"))
                DH.NgayDat = Convert.ToDateTime(row("NgayDat"))
                DH.MaMH = m_MH
                DH.MaKH = m_KH
                DH.MaLoaiGiaoDich = m_LoaiGD
                DH.MaLoaiTien = m_LoaiTien
                DatHang.Add(DH)
            Next
            Return DatHang
        End Function

        Public Sub HienThi(dGV As DataGridViewX, bN As BindingNavigator, txtMaPhieu As TextBoxX, dtNgayDat As DateTimeInput, txtSoLuong As TextBoxX, txtGiaBan As TextBoxX, _
         txtThanhTien As TextBoxX, txtGhiChu As TextBoxX, cbThanhToan As CheckBoxX, cmbTenMH As ComboBoxEx, cmbTenKH As ComboBoxEx, cmbLoaiGiaoDich As ComboBoxEx, _
         cmbLoaiTienGui As ComboBoxEx)
            Dim bS As New BindingSource()
            bS.DataSource = m_DatHangData.LayDatHang()
            Dim dT As DataTable = m_DatHangData.LayDatHang()
            Dim dathang As Boolean = Convert.ToBoolean(dT.Rows(0)("ThanhToan"))

            If dathang Then
                cbThanhToan.Checked = True
            Else
                cbThanhToan.Checked = True
            End If

            txtMaPhieu.DataBindings.Clear()
            txtMaPhieu.DataBindings.Add("Text", bS, "MaPhieu")

            dtNgayDat.DataBindings.Clear()
            dtNgayDat.DataBindings.Add("Value", bS, "NgayDat")

            txtSoLuong.DataBindings.Clear()
            txtSoLuong.DataBindings.Add("Text", bS, "SoLuong")

            txtGiaBan.DataBindings.Clear()
            txtGiaBan.DataBindings.Add("Text", bS, "GiaBan")

            txtThanhTien.DataBindings.Clear()
            txtThanhTien.DataBindings.Add("Text", bS, "ThanhTien")

            txtGhiChu.DataBindings.Clear()
            txtGhiChu.DataBindings.Add("Text", bS, "GhiChu")

            cbThanhToan.DataBindings.Clear()
            cbThanhToan.DataBindings.Add("Checked", bS, "ThanhToan")

            cmbLoaiGiaoDich.DataBindings.Clear()
            cmbLoaiGiaoDich.DataBindings.Add("SelectedValue", bS, "MaLoaiGiaoDich")

            cmbTenMH.DataBindings.Clear()
            cmbTenMH.DataBindings.Add("SelectedValue", bS, "MaMH")

            cmbTenKH.DataBindings.Clear()
            cmbTenKH.DataBindings.Add("SelectedValue", bS, "MaKH")

            cmbLoaiTienGui.DataBindings.Clear()
            cmbLoaiTienGui.DataBindings.Add("SelectedValue", bS, "MaLoaiTien")

            bN.BindingSource = bS
            dGV.DataSource = bS
        End Sub
#End Region

#Region "Them moi"
        Public Function ThemDongMoi() As DataRow
            Return m_DatHangData.ThemDongMoi()
        End Function
        Public Sub ThemMoi(m_Row As DataRow)
            m_DatHangData.ThemDatHang(m_Row)
        End Sub
#End Region

#Region "Luu"
        Public Function LuuDatHang() As Boolean
            Return m_DatHangData.LuuDatHang()
        End Function

        Public Sub LuuDatHang(maphieu As [String], mamh As [String], makh As [String], soluong As Single, giaban As Single, thanhtien As Single, _
         ngaydat As DateTime, thanhtoan As Boolean, maloaitien As [String], maloaigd As [String], ghichu As [String])
            m_DatHangData.LuuDatHang(maphieu, mamh, makh, soluong, giaban, thanhtien, _
             ngaydat, thanhtoan, maloaitien, maloaigd, ghichu)
        End Sub
#End Region

#Region "Sửa"
        Public Sub SuaDatHang(maphieu As [String], mamh As [String], makh As [String], soluong As Single, giaban As Single, thanhtien As Single, _
         ngaydat As DateTime, thanhtoan As Boolean, maloaitien As [String], maloaigd As [String], ghichu As [String])
            m_DatHangData.SuaDatHang(maphieu, mamh, makh, soluong, giaban, thanhtien, _
             ngaydat, thanhtoan, maloaitien, maloaigd, ghichu)
        End Sub
#End Region

#Region "Xóa"
        Public Sub XoaDatHang(maphieu As [String], mamh As [String], makh As [String])
            m_DatHangData.XoaDatHang(maphieu, mamh, makh)
        End Sub
#End Region
    End Class
End Namespace
